﻿Imports System.Data
Imports System.Collections.ObjectModel
Imports System.ComponentModel

Public Class Contact
    Implements INotifyPropertyChanged

    Public Sub New()
        _tel1 = ""
        _tel2 = ""
        _fax1 = ""
        _fax2 = ""
        _mobil1 = ""
        _mobil2 = ""
        _mail1 = ""
        _mail2 = ""
        _facebook = ""
        _twitter = ""
        _website = ""
    End Sub
    Private _tel1, _tel2, _fax1, _fax2, _mobil1, _mobil2, _mail1, _mail2, _facebook, _twitter, _website As String
    Public Property Telefon1 As String
        Get
            Return _tel1
        End Get
        Set(ByVal value As String)
            _tel1 = value
            NotifyPropertyChanged("Telefon1")
        End Set
    End Property
    Public Property Telefon2 As String
        Get
            Return _tel2
        End Get
        Set(ByVal value As String)
            _tel2 = value
            NotifyPropertyChanged("Telefon2")
        End Set
    End Property
    Public Property Telefax1 As String
        Get
            Return _fax1
        End Get
        Set(ByVal value As String)
            _fax1 = value
            NotifyPropertyChanged("Telefax1")
        End Set
    End Property
    Public Property Telefax2 As String
        Get
            Return _fax2
        End Get
        Set(ByVal value As String)
            _fax2 = value
            NotifyPropertyChanged("Telefax2")
        End Set
    End Property
    Public Property Mobil1 As String
        Get
            Return _mobil1
        End Get
        Set(ByVal value As String)
            _mobil1 = value
            NotifyPropertyChanged("Mobil1")
        End Set
    End Property
    Public Property Mobil2 As String
        Get
            Return _mobil2
        End Get
        Set(ByVal value As String)
            _mobil2 = value
            NotifyPropertyChanged("Mobil2")
        End Set
    End Property
    Public Property Mail1 As String
        Get
            Return _mail1
        End Get
        Set(ByVal value As String)
            _mail1 = value
            NotifyPropertyChanged("Mail1")
        End Set
    End Property
    Public Property Mail2 As String
        Get
            Return _mail2
        End Get
        Set(ByVal value As String)
            _mail2 = value
            NotifyPropertyChanged("Mail2")
        End Set
    End Property
    Public Property Facebook As String
        Get
            Return _facebook
        End Get
        Set(ByVal value As String)
            _facebook = value
            NotifyPropertyChanged("Facebook")
        End Set
    End Property
    Public Property Twitter As String
        Get
            Return _twitter
        End Get
        Set(ByVal value As String)
            _twitter = value
            NotifyPropertyChanged("Twitter")
        End Set
    End Property
    Public Property Website As String
        Get
            Return _website
        End Get
        Set(ByVal value As String)
            _website = value
            NotifyPropertyChanged("Website")
        End Set
    End Property

    Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged

    Protected Sub NotifyPropertyChanged(info As [String])
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
    End Sub
End Class

Public Class Address
    Implements INotifyPropertyChanged

    Private _street As String = ""
    Private _no As String = ""
    Private _addition As String = ""
    Private _city As String = ""
    Private _country As String = ""
    Private _zip As String = ""
    Private _forname As String = ""
    Private _surname As String = ""
    Private _salutation As String = ""
    Private _title As String = ""
    Private _companyname As String = ""

    Public Property Street As String
        Get
            Return _street
        End Get
        Set(ByVal value As String)
            _street = value
            NotifyPropertyChanged("Street")
        End Set
    End Property
    Public Property No As String
        Get
            Return _no
        End Get
        Set(ByVal value As String)
            _no = value
            NotifyPropertyChanged("No")
        End Set
    End Property
    Public Property Addition As String
        Get
            Return _addition
        End Get
        Set(ByVal value As String)
            _addition = value
            NotifyPropertyChanged("Addition")
        End Set
    End Property
    Public Property ZipCode As String
        Get
            Return _zip
        End Get
        Set(ByVal value As String)
            _zip = value
            NotifyPropertyChanged("ZipCode")
        End Set
    End Property
    Public Property City As String
        Get
            Return _city
        End Get
        Set(ByVal value As String)
            _city = value
            NotifyPropertyChanged("City")
        End Set
    End Property
    Public Property Country As String
        Get
            Return _country
        End Get
        Set(ByVal value As String)
            _country = value
            NotifyPropertyChanged("Country")
        End Set
    End Property
    Public Property Forname As String
        Get
            Return _forname
        End Get
        Set(value As String)
            _forname = value
            NotifyPropertyChanged("Forname")
        End Set
    End Property
    Public Property Surname As String
        Get
            Return _surname
        End Get
        Set(value As String)
            _surname = value
            NotifyPropertyChanged("Surname")
        End Set
    End Property
    Public Property Salutation As String
        Get
            Return _salutation
        End Get
        Set(value As String)
            _salutation = value
            NotifyPropertyChanged("Salutation")
        End Set
    End Property
    Public Property Title As String
        Get
            Return _title
        End Get
        Set(value As String)
            _title = value
            NotifyPropertyChanged("Title")
        End Set
    End Property

    Public Overrides Function ToString() As String
        Dim temp As String = ""

        If Not String.IsNullOrEmpty(CompanyName) Then
            temp += CompanyName + ", "
        End If
        If String.IsNullOrEmpty(Salutation) = False Then
            temp = Salutation + " "
        End If
        If String.IsNullOrEmpty(Forname) = False OrElse String.IsNullOrEmpty(Surname) = False Then
            If String.IsNullOrEmpty(Forname) Then
                temp += Surname + ", "
            Else
                temp += Forname
                If String.IsNullOrEmpty(Surname) = False Then
                    temp += " " + Surname + ", "
                Else
                    temp += ", "
                End If
            End If
        End If

        
        If String.IsNullOrEmpty(Addition) = False Then
            temp += Addition + ", "
        End If
        If String.IsNullOrEmpty(Street) = False Then
            temp += Street
            If String.IsNullOrEmpty(No) = False Then
                temp += " " + No + ", "
            Else
                temp += ", "
            End If
        End If
        If String.IsNullOrEmpty(ZipCode) = False OrElse String.IsNullOrEmpty(City) = False Then
            If String.IsNullOrEmpty(ZipCode) Then
                temp += City + ", "
            Else
                If String.IsNullOrEmpty(City) Then
                    temp += ZipCode + ", "
                Else
                    temp += ZipCode + " " + City + ", "
                End If
            End If
        End If
        If String.IsNullOrEmpty(Country) = False Then
            temp += Country
        Else
            temp = temp.Trim
            Do While temp.EndsWith(",")
                temp = temp.Remove(temp.Length - 1, 1)
                temp = temp.Trim()
            Loop
        End If
        Return temp
    End Function

    Public Function ToStringWithNewLine(Optional ByVal extrafreeSpace As Boolean = False, Optional ByVal showContactPersonOnCompany As Boolean = True) As String
        Dim temp As String = ""

        If Not String.IsNullOrEmpty(CompanyName) Then
            temp += CompanyName + vbCrLf
        End If
        If String.IsNullOrEmpty(Salutation) = False Then
            temp = Salutation + vbCrLf
        End If
        If String.IsNullOrEmpty(Forname) = False OrElse String.IsNullOrEmpty(Surname) = False Then
            If (showContactPersonOnCompany AndAlso Not String.IsNullOrEmpty(CompanyName)) OrElse String.IsNullOrEmpty(CompanyName) Then
                If Not String.IsNullOrEmpty(Title) Then
                    temp += Title + " "
                End If
                If String.IsNullOrEmpty(Forname) Then
                    temp += Surname + vbCrLf
                Else
                    temp += Forname
                    If String.IsNullOrEmpty(Surname) = False Then
                        temp += " " + Surname + vbCrLf
                    Else
                        temp += vbCrLf
                    End If
                End If
            End If
        End If

        If String.IsNullOrEmpty(Addition) = False Then
            temp += Addition + vbCrLf
        End If
        If String.IsNullOrEmpty(Street) = False Then
            temp += Street
            If String.IsNullOrEmpty(No) = False Then
                temp += " " + No + vbCrLf
            Else
                temp += vbCrLf
            End If
        End If
        If extrafreeSpace Then
            temp += vbCrLf
        End If
        If String.IsNullOrEmpty(ZipCode) = False OrElse String.IsNullOrEmpty(City) = False Then
            If String.IsNullOrEmpty(ZipCode) Then
                temp += City + vbCrLf
            Else
                If String.IsNullOrEmpty(City) Then
                    temp += ZipCode + vbCrLf
                Else
                    temp += ZipCode + " " + City + vbCrLf
                End If
            End If
        End If
        If String.IsNullOrEmpty(Country) = False Then
            temp += Country
        Else
            temp = temp.Trim
        End If
        Return temp

    End Function

    Public ReadOnly Property StringWithNewLine() As String
        Get
            Return ToStringWithNewLine()
        End Get
    End Property

    Public ReadOnly Property ToStringWithoutName() As String
        Get
            Dim temp As String = ""
            If String.IsNullOrEmpty(Addition) = False Then
                temp += Addition + ", "
            End If
            If String.IsNullOrEmpty(Street) = False Then
                temp += Street
                If String.IsNullOrEmpty(No) = False Then
                    temp += " " + No + ", "
                Else
                    temp += ", "
                End If
            End If
            If String.IsNullOrEmpty(ZipCode) = False OrElse String.IsNullOrEmpty(City) = False Then
                If String.IsNullOrEmpty(ZipCode) Then
                    temp += City + ", "
                Else
                    If String.IsNullOrEmpty(City) Then
                        temp += ZipCode + ", "
                    Else
                        temp += ZipCode + " " + City + ", "
                    End If
                End If
            End If
            If String.IsNullOrEmpty(Country) = False Then
                temp += Country
            Else
                temp = temp.Trim
                Do While temp.EndsWith(",")
                    temp = temp.Remove(temp.Length - 1, 1)
                    temp = temp.Trim()
                Loop
            End If
            Return temp
        End Get
    End Property

    Public Function IsEmptyOrEqual(a As Address) As Boolean
        If IsEmpty(a) Then
            Return True
        End If
        If (a.Street = Street) AndAlso a.No = No AndAlso a.Addition = Addition AndAlso a.ZipCode = ZipCode AndAlso a.City = City AndAlso a.Country = Country AndAlso a.Forname = Forname AndAlso a.Surname = Surname AndAlso a.Salutation = Salutation Then
            Return True
        End If
        Return False
    End Function

    Public Function IsEmpty(a As Address) As Boolean
        If a Is Nothing Then
            Return True
        End If
        If String.IsNullOrEmpty(a.Street) AndAlso String.IsNullOrEmpty(a.No) AndAlso String.IsNullOrEmpty(a.Addition) AndAlso String.IsNullOrEmpty(a.ZipCode) AndAlso String.IsNullOrEmpty(a.City) AndAlso String.IsNullOrEmpty(a.Country) AndAlso String.IsNullOrEmpty(a.Forname) AndAlso String.IsNullOrEmpty(a.Surname) AndAlso String.IsNullOrEmpty(a.Salutation) Then
            Return True
        End If
        Return False
    End Function

    Protected Sub NotifyPropertyChanged(info As [String])
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
    End Sub

    Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged

    '### Since Version 1.4 ###'
    ''' <summary>
    ''' This value is shown, if the property "IsCompany" inside the customer class is set to "True"
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property CompanyName As String
        Get
            Return _companyname
        End Get
        Set(value As String)
            _companyname = value
            NotifyPropertyChanged("CompanyName")
        End Set
    End Property
End Class

Public Class Unit
    Private _name, _abbr As String
    Private xml As IDatabase
    Private _UnitFormat As String

    Public Sub New()

    End Sub

    Public Sub New(ByVal Name As String, ByVal Abbr As String, ByVal _xml As IDatabase)
        _name = Name
        _abbr = Abbr
        xml = _xml
    End Sub

    Public Overridable Property Name() As String
        Set(ByVal value As String)
            _name = value
        End Set
        Get
            Return _name
        End Get
    End Property

    Public Overridable Property ShortName As String
        Set(ByVal value As String)
            _abbr = value
        End Set
        Get
            Return _abbr
        End Get
    End Property

    Public Overridable Sub Save()
        xml.SaveOrUpdateUnit(Me)
    End Sub

    Public Shared Function GetByName(ByVal Name As String, xml As IDatabase) As Unit
        Dim list As Collection(Of Unit)
        list = xml.ListOfUnits
        For Each einh As Unit In list
            If einh.Name.ToLower.Trim.Contains(Name.Trim.ToLower) OrElse einh.ShortName.ToLower.Contains(Name.Trim.ToLower) Then
                Return einh
            End If
        Next
        Throw New Exception("Einheit nicht vorhanden")
    End Function

    Public Overrides Function ToString() As String
        Return Name
    End Function

    Public Overridable Property UnitFormat As String
        Get
            If String.IsNullOrEmpty(_UnitFormat) Then
                _UnitFormat = "0.000"
            End If
            Return _UnitFormat
        End Get
        Set(value As String)
            _UnitFormat = value
        End Set
    End Property

    Public Overrides Function Equals(obj As Object) As Boolean
        Try
            Dim temp As Unit = obj
            If temp.Name = Name AndAlso temp.ShortName = ShortName Then
                Return True
            End If
            Return False
        Catch ex As Exception
            Return False
        End Try
    End Function
End Class

Public Class TaxClass
    Private _name As String
    Private _percent As Decimal
    Private xml As IDatabase

    Public Sub New()

    End Sub

    Public Sub New(ByVal Name As String, ByVal TaxRate As String, ByVal _xml As IDatabase)
        _name = Name
        Try
            _percent = CDec(TaxRate)
        Catch ex As Exception
            _percent = CDec(TaxRate.Replace("%", "").Trim)
        End Try
        xml = _xml
    End Sub

    Public Overridable Property Name As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property

    Public Overridable Property TaxRate As Decimal
        Get
            Return _percent
        End Get
        Set(ByVal value As Decimal)
            _percent = value
        End Set
    End Property

    Public Overridable ReadOnly Property TaxRateString As String
        Get
            Return TaxRate.ToString("0.00") + " %"
        End Get
    End Property

    Public Overridable Sub Save()
        xml.SaveOrUpdateTax(Me)
    End Sub

    Public Overrides Function Equals(obj As Object) As Boolean
        Try
            Dim temp As TaxClass = obj
            If temp.Name = Name Then
                Return True
            End If
            Return False
        Catch ex As Exception
            Return False
        End Try
    End Function

    Public Shared Function GetByName(ByVal Name As String, xml As IDatabase) As TaxClass
        Dim list As ObservableCollection(Of TaxClass)
        list = xml.ListOfTaxes
        For Each einh As TaxClass In list
            If einh.Name.ToLower.Contains(Name.ToLower) Then
                Return einh
            End If
        Next
        Throw New Exception("Steuerklasse nicht vorhanden")
    End Function

    Public Shared Function GetByRate(ByVal Rate As Decimal, xml As IDatabase) As TaxClass
        Dim list As ObservableCollection(Of TaxClass)
        list = xml.ListOfTaxes
        For Each einh As TaxClass In list
            If einh.TaxRate = Rate Then
                Return einh
            End If
        Next
        Throw New Exception("Steuerklasse nicht vorhanden")
    End Function
End Class

<Flags()> Public Enum Dokumentart As Integer
    Rechnung = 1
    Lieferschein = 2
    Gutschrift = 3
    Angebot = 4
End Enum

Public Enum Preisgruppe As Integer
    Preisgruppe1 = 0
    Preisgruppe2 = 1
    Preisgruppe3 = 2
End Enum